import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/404',
    name: '404',
    component: ()=>import('../views/404.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: ()=>import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/',
    name: 'manager',
    component: () => import('../views/Manager.vue'),
    redirect: "/home",
    children:[
      {path :'/home',name: 'Home',component: ()=>import('../views/manager/Home.vue')},
      {path :'/address',name: 'Address',component: ()=>import('../views/manager/Address.vue')},
      {path :'/password',name: 'Password',component: ()=>import('../views/manager/Password.vue')},
      {path :'/sellhot',name: 'SellHot',component: ()=>import('../views/manager/SellHot.vue')},
      {path :'/newproduct',name: 'NewProduct',component: ()=>import('../views/manager/NewProduct.vue')},
      {path: '/goodsdetail',name: 'GoodsDetail',component: ()=>import('../views/manager/GoodsDetail.vue')},
      {path: '/shoppingcart',name: 'ShoppingCart',component: ()=>import('../views/manager/ShoppingCart.vue')},
      {path: '/merchantOrderManage',name: 'merchantOrderManage',component: ()=>import('../views/manager/merchantOrderManage.vue')},
      {path: '/goodscategory',name: 'GoodsCategory',component: ()=>import('../views/manager/GoodsCategory.vue')},
      {path: '/hot',name: 'Hot',component: ()=>import('../views/manager/Hot.vue')},
      {path: '/new',name: 'New',component: ()=>import('../views/manager/New.vue')},
      {path: '/selectgoods',name: 'SelectGoods',component: ()=>import('../views/manager/SelectGoods.vue')},
    ],

  },
  {
    path: '/controller',
    name: 'Controller',
    component: ()=>import('../views/Controller.vue'),
    redirect: "/goods",
    children: [
      {path: '/goods',name: 'Goods',component: ()=>import('../views/controller/Goods.vue')},
      {path: '/order',name: 'Order',component: ()=>import('../views/controller/Order.vue')},
      {path: '/category',name: 'Category',component: ()=>import('../views/controller/Category.vue')},
      {path: '/chart',name: 'Chart',component: ()=>import('../views/controller/Chart.vue')},
      {path: '/adminpassword',name: 'AdminPassword',component: ()=>import('../views/controller/AdminPassword.vue')},
      {path: '/logs',name: 'Logs',component: ()=>import('../views/controller/Logs.vue')},
      {path: '/user',name: 'User',component: ()=>import('../views/controller/User.vue')},
    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//默认的三个参数
router.beforeEach((to, from, next) => {
  //to 要到达的路由,.path获取url
  //from 从哪个路由出发，.path获取url
  //next 帮助跳转路由的函数

  const matchedRoutes = to.matched
  if (to.name !== '404' && matchedRoutes.length === 0) {
    // 跳转的页面在路由配置中不存在，跳转到404页面
    next('/404')
  } else {
    // 跳转的页面存在或是404页面，继续路由跳转
    next()
  }


  // const user = JSON.parse(localStorage.getItem('user') || '{}')
  // let isAdmin = user.role === '管理员'
  // let  userPath = ['/']
  // let adminPath = ['/controller']
  // console.log("路由守卫")
  // if (!user.role) {
  //   console.log("用户未登录")
  //   // 用户未登录，可以正常跳转，权限访问交给jwt
  //   if(to.matched.length === 0){
  //     next('/404')
  //   }
  //     next()
  //
  // } else if (isAdmin) {
  //   console.log("管理员来了")
  //   // 管理员访问不存在的页面，跳转到404页面
  //   if (adminPath.includes(to.path)) {
  //     console.log("成功到达")
  //     next()
  //   } else {
  //     console.log("访问失败")
  //     next('/404')
  //   }
  // } else {
  //   console.log("用户来了")
  //   // 用户访问不存在的页面，跳转到404页面
  //   if (userPath.includes(to.path)) {
  //     console.log("成功到达")
  //     next()
  //   } else {
  //     console.log("访问失败")
  //     next('/404')
  //   }
  // }

})


export default router
